﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;
using AcademicPlanner.Framework.Helpers;

namespace AcademicPlanner.Presentation.Views.Shared
{
	/// <summary>
	/// Possible scenarios of a <c>AcademicPlanForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public enum AcademicPlanFormScenario
	{
		EDIT
	}


	/// <summary>
	/// AcademicPlanFormModel represents a model object for a <c>AcademicPlanForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public class AcademicPlanFormModel : FormModel
	{
		/// <summary>
		/// Gets or sets the scenario to use the <c>AcademicPlanForm</c> form under.
		/// </summary>
		/// <since>1.0</since>
		public AcademicPlanFormScenario Scenario { get; set; }

		/// <summary>
		/// Gets or sets the <c>AcademicPlan</c> whose data to save.
		/// </summary>
		/// <since>1.0</since>
		public AcademicPlan AcademicPlan { get; set; }
	}


	/// <summary>
	/// AcademicPlanForm represents a form to save data about an <c>AcademicPlan</c>.
	/// </summary>
	/// <since>1.0</since>
	public partial class AcademicPlanForm : Form<AcademicPlanFormModel>
	{
		/// <summary>
		/// Gets the model object for this form control.
		/// </summary>
		/// <since>1.0</since>
		public new AcademicPlanFormModel Model { get { return (AcademicPlanFormModel)base.Model; } }

		/// <summary>
		/// Gets or sets the value of the credit units field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string CreditUnitsValue { get; set; }

		/// <summary>
		/// Gets or sets the values of the academic term name fields of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<string> AcademicTermNameValues { get; set; }

		/// <summary>
		/// Gets or sets the values of the academic term position in the year fields of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<string> AcademicTermOrderNumValues { get; set; }

		/// <summary>
		/// Gets or sets the values of the division name fields of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<string> CourseDivisionNameValues { get; set; }

		/// <summary>
		/// Gets or sets the values of the division category name fields of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<IList<string>> CourseDivisionCategoryNameValues { get; set; }

		/// <summary>
		/// Gets or sets the values of the division category code fields of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<IList<string>> CourseDivisionCategoryCodeValues { get; set; }

		/// <summary>
		/// Gets or sets the values of the index of the last existing division category fields of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<int> LastCourseDivisionCategoryIndex { get; set; }

		/// <summary>
		/// Gets or sets the value of the index of the last existing division field of this form.
		/// </summary>
		/// <since>1.0</since>
		public int LastCourseDivisionIndex { get; set; }

		/// <summary>
		/// Gets or sets the value of the index of the last existing academic term field of this form.
		/// </summary>
		/// <since>1.0</since>
		public int LastAcademicTermIndex { get; set; }


		/// <summary>
		/// Handles the page load event for this form.
		/// </summary>
		/// <remarks>
		/// This methods intializes or customizes the form's properties
		/// based on the model object's <c>Scenario</c>.
		/// </remarks>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details about the event.</param>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			method = FormMethod.Post;

			actionName = "Save";
			controllerName = "MyPlan";
			routeValues = new object { };
			Name = "savePlanForm";
			Title = Tr("Change Plan Settings");
			SubmitButtonValue = Tr("Save");

			CreditUnitsValue = Model.AcademicPlan.CreditUnits.ToString();
			AcademicTermNameValues = new List<string>();
			AcademicTermOrderNumValues = new List<string>();
			LastAcademicTermIndex = -1;
			foreach (var vTerm in Model.AcademicPlan.Terms.ToList())
			{
				AcademicTermNameValues.Add(vTerm.Name);
				AcademicTermOrderNumValues.Add(vTerm.OrderNum.ToString());
				LastAcademicTermIndex++;
			}
			CourseDivisionNameValues = new List<string>();
			CourseDivisionCategoryNameValues = new List<IList<string>>();
			CourseDivisionCategoryCodeValues = new List<IList<string>>();
			LastCourseDivisionIndex = -1;
			LastCourseDivisionCategoryIndex = new List<int>();
			foreach (var vDiv in Model.AcademicPlan.Divisions.ToList())
			{
				CourseDivisionNameValues.Add(vDiv.Name);
				var vCatNames = new List<string>();
				var vCatCodes = new List<string>();
				var vLastCatIndex = -1;
				foreach (var vCat in vDiv.Categories.ToList())
				{
					vCatNames.Add(vCat.Name);
					vCatCodes.Add(vCat.Code);
					vLastCatIndex++;
				}
				for (var vJ = 0; vJ < 5; vJ++)
				{
					vCatNames.Add("");
					vCatCodes.Add("");
				}
				CourseDivisionCategoryNameValues.Add(vCatNames);
				CourseDivisionCategoryCodeValues.Add(vCatCodes);
				LastCourseDivisionCategoryIndex.Add(vLastCatIndex);
				LastCourseDivisionIndex++;
			}
			for (var vI = 0; vI < 5; vI++)
			{
				CourseDivisionNameValues.Add("");
				AcademicTermNameValues.Add("");
				AcademicTermOrderNumValues.Add("");
				var vCatNames = new List<string>();
				var vCatCodes = new List<string>();
				for (var vJ = 0; vJ < 5; vJ++)
				{
					vCatNames.Add("");
					vCatCodes.Add("");
				}
				CourseDivisionCategoryNameValues.Add(vCatNames);
				CourseDivisionCategoryCodeValues.Add(vCatCodes);
				LastCourseDivisionCategoryIndex.Add(-1);
			}
		}
	}
}